﻿;/////////////////////////////////////////////////////////////;
;                                                             ;
;            SimpleDX11 Shader Compiler Procedures            ;
;                                                             ;
;                                                             ;
;                   (c) 2013 SimpleDX Team                    ;
;                                                             ;
;                         License: MIT                        ;
;                                                             ;
;/////////////////////////////////////////////////////////////;




;-//    Private structures    //


Structure Compiler_Template
  *vTable
  *p.Shader
  *SD3D.Screen3D
  
  ShaderBlob.ID3D10Blob
  ErrorBlob.ID3D10Blob   
  
  Type.l
EndStructure


;-//    Constructor    //
Procedure New_Compiler(*SD3D.Screen3D)
  Protected *this.Compiler_Template
 
  *this = AllocateMemory(SizeOf(Compiler_Template))
  InitializeStructure(*this, Compiler_Template)
  
  If *this
    *this\vTable    = ?Compiler_Class    
    *this\p         = *this
    *this\SD3D       = *SD3D   
    ProcedureReturn *this
  EndIf  
  ProcedureReturn #False
EndProcedure

;-//    Public procedures    //

; Compile a Shader from file
Procedure Compiler_CompileShaderFromFile(*this.Compiler_Template, File.s, EntryPoint.s, SV.s)
  Protected Result.l  = #S_OK
  
  Protected *FileStr          = StringToByteString(File)
  Protected *EntryPointStr    = StringToByteString(EntryPoint)
  Protected *ShaderVersionStr = StringToByteString(SV)
  Protected *Blob.ID3D10Blob, Type.l, CompileFlags.l = #D3DCOMPILE_ENABLE_STRICTNESS
  
  If *this\ShaderBlob : *this\ShaderBlob\Release() : EndIf
  If *this\ErrorBlob : *this\ErrorBlob\Release() : EndIf  
  *this\ErrorBlob = #Null  
  
  CompilerIf #PB_Compiler_Debugger
    CompileFlags | #D3DCOMPILE_DEBUG
  CompilerEndIf    
  
  Result = D3DX11CompileFromFileA(*FileStr, #Null, #Null,  *EntryPointStr, *ShaderVersionStr, CompileFlags , 0, 0, @*this\ShaderBlob, @*this\ErrorBlob, 0)
  If Result
    ProcedureReturn Result
  EndIf  
  
  FreeMemory(*FileStr)
  FreeMemory(*EntryPointStr)
  FreeMemory(*ShaderVersionStr) 
  
  ProcedureReturn Result
EndProcedure

; Set the shader type
; 1 = VertexShader, 2 = PixelShader
Procedure Compiler_SetType(*this.Compiler_Template, Type.l)
    *this\Type = Type
EndProcedure
  
; Get the shader type
; 1 = VertexShader, 2 = PixelShader
Procedure Compiler_GetType(*this.Compiler_Template)
    ProcedureReturn *this\Type 
  EndProcedure
  
; Get the ShaderBlob
Procedure Compiler_GetShaderBlob(*this.Compiler_Template)
    ProcedureReturn *this\ShaderBlob
EndProcedure

; Get the ErrorBlob
Procedure Compiler_GetErrorBlob(*this.Compiler_Template)
    ProcedureReturn *this\ErrorBlob
EndProcedure

; Returns the Pointer of the ErrorBlob
Procedure Compiler_GetErrorBlobPointer(*this.Compiler_Template)
  If *this\ErrorBlob : ProcedureReturn *this\ErrorBlob\GetBufferPointer() :EndIf
  ProcedureReturn 0
EndProcedure

; Returns the Size of the ErrorBlob
Procedure Compiler_GetErrorBlobSize(*this.Compiler_Template)
  If *this\ErrorBlob : ProcedureReturn *this\ErrorBlob\GetBufferSize() :EndIf
  ProcedureReturn 0
EndProcedure


Procedure Compiler_Release(*this.Compiler_Template) 
  If *this\ShaderBlob : *this\ShaderBlob\Release()  : EndIf
  If *this\ErrorBlob  : *this\ErrorBlob\Release()   : EndIf  
  FreeMemory(*this)
EndProcedure

;-//    Private procedures    //


;/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;-//    DataSection    //
DataSection
  Compiler_Class:
  Data.i @Compiler_CompileShaderFromFile()
  Data.i @Compiler_SetType()
  Data.i @Compiler_GetType()
  Data.i @Compiler_GetShaderBlob()
  Data.i @Compiler_GetErrorBlob()
  Data.i @Compiler_GetErrorBlobPointer()
  Data.i @Compiler_GetErrorBlobSize()
  Data.i @Compiler_Release()
EndDataSection
; IDE Options = PureBasic 5.20 beta 14 LTS (Windows - x64)
; CursorPosition = 38
; FirstLine = 1
; Folding = --
; EnableUnicode
; EnableXP